package com.icinfo.cloud.provider.punish.strategy.document;

import com.icinfo.cloud.provider.punish.document.annotations.DocumentParamConvert;
import com.icinfo.cloud.provider.punish.document.dto.PrintDocumentBuildDto;
import com.icinfo.cloud.provider.punish.print.vo.ImageContentVo;
import com.icinfo.cloud.provider.punish.print.vo.PrintContentVo;
import com.icinfo.cloud.provider.punish.print.vo.PrintDocumentVo;
import com.icinfo.cloud.provider.punish.print.vo.TitleOrContentVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * 责令停止违法行为通知书文书参数
 *
 * @author Junfeng.Chen
 * @date 2024/1/15 8:49
 **/
@Data
@Slf4j
public class ZltzwfxwtzsDocumentParam implements BaseDocumentParam {
    /**
     * 单位名称
     */
    @DocumentParamConvert(narrow = "orgStandardName", standard = "orgStandardName")
    private String orgStandardName;

    /**
     * 文书号
     */
    @DocumentParamConvert(narrow = "documentNumber", standard = "documentNumber")
    private String documentNumber;

    /**
     * 责令停止违法，发生时间
     */
    @DocumentParamConvert(narrow = "ZLTZWFXW_HAPPEN_TIME", standard = "ZLTZWFXW_HAPPEN_TIME")
    private String happenTime;

    /**
     * 当事人名称
     */
    @DocumentParamConvert(narrow = "partyName", standard = "partyName")
    private String partyName;

    /**
     * 案发地址，宽幅不包含该字段的直接映射
     */
    @DocumentParamConvert(narrow = "addressWithLatAndLgt", standard = "addressWithLatAndLgt")
    private String addressWithLatAndLgt;

    /**
     * 违法行为
     */
    @DocumentParamConvert(narrow = "zltzwfxwtzs_wfxw", standard = "zltzwfxwtzs_wfxw")
    private String wfwx;

    /**
     * 违则
     */
    @DocumentParamConvert(narrow = "ZLTZWFXW_ILLEGAL_BASIS", standard = "ZLTZWFXW_ILLEGAL_BASIS")
    private String illegalBasis;

    /**
     * 依据
     */
    @DocumentParamConvert(narrow = "ZLTZWFXW_YJ", standard = "ZLTZWFXW_YJ")
    private String yj;

    /**
     * 当事人签名
     */
    @DocumentParamConvert(narrow = "signFingerprintPic", standard = "signFingerprintPic")
    private String partySign;

    /**
     * 联系电话
     */
    @DocumentParamConvert(narrow = "ZLTZWFXWTZ_mobile", standard = "ZLTZWFXWTZ_mobile")
    private String linkPhone;

    /**
     * 主要执法人员签字图片
     */
    @DocumentParamConvert(narrow = "mainHandleSignPic", standard = "mainHandleSignPic")
    private String mainHandleSignPic;

    /**
     * 协办执法人员签字图片
     */
    @DocumentParamConvert(narrow = "assistHandleSignPic", standard = "assistHandleSignPic")
    private String assistHandleSignPic;

    /**
     * 不签字原因，为null代表没有不签字的逻辑，否则为有不签字逻辑
     */
    @DocumentParamConvert(narrow = "notSignReason", standard = "notSignReason")
    private String notSignReason;

    /**
     * 签章单位名称
     */
    @DocumentParamConvert(narrow = "orgStandardNameSeal", standard = "orgStandardNameSeal")
    private String orgStandardNameSeal;

    /**
     * 签章时间
     */
    @DocumentParamConvert(narrow = "sealDate", standard = "sealDate")
    private String sealDate;

    /**
     * json打印时使用的签章图片url
     */
    @DocumentParamConvert(narrow = "sealPic", standard = "sealPic")
    private String sealPic;

    @Override
    public PrintDocumentVo assemblyPrintContent(PrintDocumentBuildDto dto) {
        PrintContentVo textContent = new PrintContentVo();
        textContent.setTitle(assemblyTitle());
        textContent.setPartyInfo(assemblyPartyInfo());
        textContent.setBodyContent(assemblyBodyContent());
        textContent.setImageInfo(assemblyImageInfo(dto));
        return new PrintDocumentVo(null, textContent);
    }

    /**
     * 组装图片板块
     *
     * @param dto
     * @return
     */
    private List<ImageContentVo> assemblyImageInfo(PrintDocumentBuildDto dto) {
        List<ImageContentVo> imageInfo = new LinkedList<>();
        //当事人签字
        imageInfo.add(new ImageContentVo()
                .setDescription("当事人签字：")
                //url转base64存入
                .setImageBase64(Collections.singletonList(convertImageUrlToBase64(partySign)))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        imageInfo.add(new ImageContentVo()
                .setDescription("联系电话：" + Optional.ofNullable(linkPhone).orElse(""))
                .setImageBase64(Collections.singletonList(""))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        //执法签字
        imageInfo.add(new ImageContentVo()
                .setDescription("执法签字：")
                .setImageBase64(Arrays.asList(convertImageUrlToBase64(mainHandleSignPic), convertImageUrlToBase64(assistHandleSignPic)))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        //获取签章图片
        addDeptSealPic(imageInfo, sealPic, dto);
        //当事人未签字原因
        addNotSignReason(imageInfo, notSignReason);
        return imageInfo;
    }

    /**
     * 组装内容
     *
     * @return
     */
    private List<TitleOrContentVo> assemblyBodyContent() {
        List<TitleOrContentVo> bodyContent = new LinkedList<>();
        String content1 = "    经查，你（单位）于" + Optional.ofNullable(happenTime).orElse("") + "在"
                + getNarrowCaseAddress(addressWithLatAndLgt) + "进行" + Optional.ofNullable(wfwx).orElse("")
                + "的行为，违反了" + Optional.ofNullable(illegalBasis).orElse("") + "的规定。";
        String content2 = "    依据" + Optional.ofNullable(yj).orElse("") + "的规定，责令你（单位）立即停止该违法行为。";
        bodyContent.add(new TitleOrContentVo(content1).initNormalStyle());
        bodyContent.add(new TitleOrContentVo(content2).initNormalStyle());
        return bodyContent;
    }

    /**
     * 组装当事人信息
     *
     * @return
     */
    private List<TitleOrContentVo> assemblyPartyInfo() {
        List<TitleOrContentVo> partyInfo = new LinkedList<>();
        //当事人
        partyInfo.add(new TitleOrContentVo(Optional.ofNullable(partyName).orElse("") + "：").initNormalStyle());
        return partyInfo;
    }

    /**
     * 组装头部信息
     *
     * @return
     */
    private List<TitleOrContentVo> assemblyTitle() {
        List<TitleOrContentVo> title = new LinkedList<>();
        title.add(new TitleOrContentVo(Optional.ofNullable(orgStandardName).orElse("")).initTitleStyle());
        title.add(new TitleOrContentVo("责令停止违法行为通知书").initTitleStyle());
        title.add(new TitleOrContentVo(Optional.ofNullable(documentNumber).orElse("")).initDocumentNumberStyle());
        return title;
    }
}
